package com.monitor.filter;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

/**
 * 网址过滤器，用于拦截不良网站
 */
public class UrlFilter implements ContentFilter {
    private FilterSensitivity sensitivity = FilterSensitivity.MEDIUM;
    private List<String> blacklist = new ArrayList<>();
    private List<String> whitelist = new ArrayList<>();
    
    public UrlFilter() {
        // 初始化默认黑白名单
        initDefaultLists();
    }
    
    private void initDefaultLists() {
        // 添加一些默认的黑名单网址
        blacklist.add("gambling.com");
        blacklist.add("adult-content.com");
        blacklist.add("violence-games.com");
        
        // 添加一些默认的白名单网址
        whitelist.add("edu.cn");
        whitelist.add("gov.cn");
        whitelist.add("wikipedia.org");
    }
    
    @Override
    public boolean containsInappropriateContent(String url) {
        if (url == null || url.isEmpty()) {
            return false;
        }
        
        // 首先检查白名单
        for (String whiteUrl : whitelist) {
            if (url.contains(whiteUrl)) {
                return false; // 在白名单中，直接放行
            }
        }
        
        // 根据灵敏度级别调整匹配策略
        switch (sensitivity) {
            case HIGH:
                return highSensitivityCheck(url);
            case MEDIUM:
                return mediumSensitivityCheck(url);
            case LOW:
                return lowSensitivityCheck(url);
            default:
                return mediumSensitivityCheck(url);
        }
    }
    
    private boolean highSensitivityCheck(String url) {
        // 高灵敏度：部分匹配即可
        for (String blackUrl : blacklist) {
            if (url.contains(blackUrl)) {
                return true;
            }
        }
        return false;
    }
    
    private boolean mediumSensitivityCheck(String url) {
        // 中灵敏度：域名匹配
        String domain = extractDomain(url);
        for (String blackUrl : blacklist) {
            if (domain.contains(blackUrl) || blackUrl.contains(domain)) {
                return true;
            }
        }
        return false;
    }
    
    private boolean lowSensitivityCheck(String url) {
        // 低灵敏度：完全匹配
        String domain = extractDomain(url);
        for (String blackUrl : blacklist) {
            if (domain.equals(blackUrl)) {
                return true;
            }
        }
        return false;
    }
    
    private String extractDomain(String url) {
        // 简单的域名提取
        url = url.toLowerCase();
        if (url.startsWith("http://")) {
            url = url.substring(7);
        } else if (url.startsWith("https://")) {
            url = url.substring(8);
        }
        
        int slashIndex = url.indexOf('/');
        if (slashIndex != -1) {
            url = url.substring(0, slashIndex);
        }
        
        return url;
    }
    
    @Override
    public String getFilterName() {
        return "网址过滤器";
    }
    
    @Override
    public String getFilterDescription() {
        return "基于预置的黑名单库和白名单库拦截或放行特定网站";
    }
    
    @Override
    public void setSensitivity(FilterSensitivity sensitivity) {
        this.sensitivity = sensitivity;
    }
    
    @Override
    public FilterSensitivity getSensitivity() {
        return sensitivity;
    }
    
    /**
     * 添加黑名单网址
     * @param url 要添加的网址
     */
    public void addToBlacklist(String url) {
        if (url != null && !url.isEmpty() && !blacklist.contains(url)) {
            blacklist.add(url);
        }
    }
    
    /**
     * 从黑名单中移除网址
     * @param url 要移除的网址
     */
    public void removeFromBlacklist(String url) {
        blacklist.remove(url);
    }
    
    /**
     * 添加白名单网址
     * @param url 要添加的网址
     */
    public void addToWhitelist(String url) {
        if (url != null && !url.isEmpty() && !whitelist.contains(url)) {
            whitelist.add(url);
        }
    }
    
    /**
     * 从白名单中移除网址
     * @param url 要移除的网址
     */
    public void removeFromWhitelist(String url) {
        whitelist.remove(url);
    }
    
    /**
     * 获取黑名单列表
     * @return 黑名单列表
     */
    public List<String> getBlacklist() {
        return new ArrayList<>(blacklist);
    }
    
    /**
     * 获取白名单列表
     * @return 白名单列表
     */
    public List<String> getWhitelist() {
        return new ArrayList<>(whitelist);
    }
    
    /**
     * 更新黑名单
     * @param newBlacklist 新的黑名单列表
     */
    public void updateBlacklist(List<String> newBlacklist) {
        this.blacklist.clear();
        if (newBlacklist != null) {
            for (String url : newBlacklist) {
                addToBlacklist(url);
            }
        }
    }
    
    /**
     * 清空黑名单
     */
    public void clearBlacklist() {
        blacklist.clear();
    }
    
    /**
     * 清空白名单
     */
    public void clearWhitelist() {
        whitelist.clear();
    }
}